package com.kunpeng.weibo.weixin;

import android.content.Context;
import android.util.Log;

import com.kunpeng.weibo.weixin.api.SendImage_API;
import com.kunpeng.weibo.weixin.api.SendText_API;
import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.ConstantsAPI;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 微信
 * 
 * @author Lee
 */
public class Weixin implements IWXAPIEventHandler {
	public static String	WEIXIN_APP_ID;	//
	public static String	WEIXIN_APP_KEY; //

	private static Weixin	mWeixin;

	private IWXAPI			mWeixinApi;

	private Weixin() {

	}

	public synchronized static Weixin getInstance() {
		if (mWeixin == null) {
			mWeixin = new Weixin();
		}
		return mWeixin;
	}

	public void init(Context context, String appId, String appKey) {
		WEIXIN_APP_ID = appId;
		WEIXIN_APP_KEY = appKey;
		mWeixinApi = WXAPIFactory.createWXAPI(context, WEIXIN_APP_ID);
		mWeixinApi.registerApp(WEIXIN_APP_ID);
	}

	// 微信发送请求到第三方应用时，会回调到该方法
	@Override
	public void onReq(BaseReq req) {
		Log.i("lijinzhe", "onReq");
		switch (req.getType()) {
		case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
			// goToGetMsg();
			break;
		case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
			// goToShowMsg((ShowMessageFromWX.Req) req);
			break;
		default:
			break;
		}
	}

	// 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
	@Override
	public void onResp(BaseResp resp) {
		Log.i("lijinzhe", "onResp");
		String result;

		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = "发送成功";
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = "发送取消";
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = "发送被拒绝";
			break;
		default:
			result = "发送返回";
			break;
		}
	}

	public SendText_API getSendText_API() {
		return new SendText_API(mWeixinApi);
	}

	public SendImage_API getSendImage_API() {
		return new SendImage_API(mWeixinApi);
	}

}
